/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest


* ---------
* Table A21
* ---------

* Globals
global dep "ln_time fraud c1_dlog_val c1_dlog_tax htrl_f_int"
global controls "tax_rate risk red_i mixed_dec differentiated valitrade_advice"
global excess "rLL rLH rHL rHH"

* Matrix
mat tableA21 = J(22,5,.)
mat tableA21_T = J(1,5,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {

	** Regression
	qui: reghdfe `x' eis_f $excess $controls countryorig* max_twodigit* if sample_reg==1 , absorb(key_dec ym_* insp_f) vce(cluster insp_f key_dec)

	mat tableA21[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _se[eis_f]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _b[rLL]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _se[rLL]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _b[rLH]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _se[rLH]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _b[rHL]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _se[rHL]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _b[rHH]
	local i = `i' + 1
	mat tableA21[`i',`j'] = _se[rHH]

	** P-values of ttests
	local i = `i' + 8
	qui: test rLH = rLL
	matrix tableA21[`i',`j']=r(p)
	local i = `i' + 1
	qui: test rHL = rLL
	matrix tableA21[`i',`j']=r(p)
	local i = `i' + 1
	qui: test rLH = rHH
	matrix tableA21[`i',`j']=r(p)

	** Observations and R2
	mat tableA21_T[1,`j'] = e(N)
	local i = `i' + 2
	mat tableA21[`i',`j'] = e(r2)
	local i = `i' -21
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A21.xlsx", replace sheet(TableA21) // create a new excel spreadsheet
putexcel B4 = matrix(tableA21), nformat(0.000) hcenter vcenter
putexcel B24 = matrix(tableA21_T), nformat(number_sep) hcenter vcenter

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Column names
putexcel A2 = "Dependent variable:", left vcenter bold
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Time" "Fraud" "\Delta log value" "\Delta log tax" "Hyp. tax revenue losses" "'
forvalues i = 1(1)5 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel `y'2 = "`x'", hcenter vcenter bold	
	putexcel `y'3 = "(`i')", hcenter vcenter

	forvalues j = 14(1)19 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
}

* Row names
local j = 4
local names `" "Excess interaction share" "Re-assignment No Excess -> No Excess (RNN)" "Re-assignment No Excess -> Excess (RNE)" "Re-assignment Excess->No Excess (REN)" "Re-assignment Excess -> Excess (REE)" "'
forvalues i = 1(1)5 {

	local x: word `i' of `names'

	putexcel A`j' = "`x'", left vcenter
	local j = `j'+2
}

local j = 14
local names `" "Declaration characteristics" "Inspector fixed effects" "Broker fixed effects" "Source country fixed effects" "HS2-product fixed effects" "Month-year fixed effects" "P-values" "test for difference (RNN)=(RNE)" "test for difference (REN)=(RNN)" "test for difference (RNE)=(REE)" "Observations" "R-squared" "'
forvalues i = 1(1)12 {

	local x: word `i' of `names'
	
	if "`i'" == "8" | "`i'" == "9" | "`i'" == "10" {
		putexcel A`j' = "`x'", left vcenter italics
	}
	
	else {
		putexcel A`j' = "`x'", left vcenters
	}
	
	local j = `j'+1
}


* -------------------------------- ENDS HERE -------------------------------- *